⚡️ Speed up function rgb_to_hsv by 162%
#245
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
📄 162% (1.62x) speedup for
rgb_to_hsvinlib/matplotlib/colors.py⏱️ Runtime :
94.8 milliseconds→36.2 milliseconds(best of10runs)📝 Explanation and details
The optimized code achieves a 162% speedup by eliminating expensive operations and reducing memory allocations. The key optimizations are:
What was optimized:
Replaced expensive
np.ptp()with direct subtraction: The original usednp.ptp(arr, -1)(30.6% of runtime), which internally computes both max and min. The optimized version computesarr_max - arr_mindirectly, reusing the already-computed min/max values.Used faster min/max functions: Replaced
arr.max(-1)withnp.maximum.reduce([r, g, b])for the 3-channel case, which is more efficient for small fixed dimensions.Eliminated redundant indexing operations: The original performed expensive boolean array indexing three times (
out[idx, 0] = ...taking 13.1-13.2% each). The optimized version precomputes all arithmetic using vectorized operations without=parameters, then assigns results in bulk.Reduced memory allocations: Used
np.empty_like()instead ofnp.zeros_like()where initialization isn't needed, and leveraged NumPy'sout=parameter to reuse buffers and avoid temporary arrays.Why it's faster:
np.ptp()operation that was the single largest bottleneckImpact on workloads:
The function is called from
blend_hsv()for shaded relief visualization, processing image data arrays. The optimization particularly benefits large image processing workloads - test results show 77-88% speedups on large batches (1000+ colors) while maintaining similar performance on small inputs, making it ideal for the image processing context where this function is used.✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
test_colors.py::test_rgb_hsv_round_trip🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-rgb_to_hsv-mja51eypand push.